package com.hyx.spring.config.condition;

import com.hyx.spring.bean.Company;
import com.hyx.spring.bean.Department;
import com.hyx.spring.bean.Employee;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @Author hyx
 * @Description ImportBeanDefinitionRegistrar的实现类
 * @Date 2024/3/10 17:33
 * @Version 1.0
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    /**
     *
     * @param importingClassMetadata 当前类的注解信息
     * @param registry  BeanDefinition注册类
     *  通过调用BeanDefinitionRegistry接口的registerBeanDefinition()方法，可以将所有需
     * 要添加到容器中的bean注入到容器中。
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry);

        boolean employee = registry.containsBeanDefinition(Employee.class.getName());
        boolean department = registry.containsBeanDefinition(Department.class.getName());
        if(employee && department){
            RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Company.class);
            //注入
            registry.registerBeanDefinition("company",rootBeanDefinition);

        }

    }
}
